package noobteam.deartrainer.controller;


import noobteam.deartrainer.service.UserInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 高乾威
 * 2021-6-23 19：30 用户注册功能
 */
@RestController
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;
    @Resource
    private BudgetInfoController budgetInfoController;

    @RequestMapping("/user_signUp")
    public String save(@RequestParam Map<String,String> map){
        String msg="注册失败";
        boolean flag=userInfoService.doSave(map);
        if(flag){
            msg="注册成功";
        }
        return msg;
    }

    @RequestMapping("/user_signIn")
    public String checkSignIn(@RequestParam Map<String,String> map){

        String name = map.get("userName");
        String password = map.get("userPassword");
        List<Map<String,Object>> list = userInfoService.getName();
        for(Map<String,Object> temp : list){
            if(temp.get("userName").equals(name)){
                Map<String, Object> userPassWordByName = userInfoService.getUserPassWordByName(name);
                if (userPassWordByName.get("userPassword").equals(password)){
                    return "登录成功";
                }
                else{
                    return "密码错误";
                }
            }
        }
        return "该用户名不存在，请检查";
    }

    @RequestMapping("/sign_repeateNameCheck")
    public String isNameValid(@RequestParam Map<String,String> js){
        List<Map<String,Object>> list = userInfoService.getName();
        String msg = "OK";
        for(Map<String,Object> map : list){
            if(map.get("userName").equals(js.get("userName"))){
                return "该用户名已存在，请更改";
            }
        }
        return msg;
    }

    @RequestMapping("/getAllUserInfo")
    public List<Map<String,Object>> findAllUserInfo(){
        List<Map<String,Object>> list = userInfoService.getAllInfo();
        return list;
    }

    @RequestMapping("/getUserInfoByName")
    public Map<String, Object> findUserInfoByName(@RequestParam Map<String, String> userName){
        String name = userName.get("userName");
        Map<String, Object> map = userInfoService.getUserInfoByName(name);
        return map;
    }

    @RequestMapping("/getUserInfoById")
    public Map<String, Object> findUserInfoById(@RequestParam int userId){
        Map<String, Object> map = userInfoService.getUserInfoById(userId);
        return map;
    }

    @RequestMapping("/editUserInfo")
    public String updateH(@RequestParam Map<String,String> map){
        String msg="修改失败";
        boolean flag = userInfoService.doUpdatePersonInfo(map);
        if(flag){
            msg="修改成功";
        }
        return msg;
    }

    @RequestMapping("/updatePhoto")
    public  String updatePhoto(@RequestParam Map<String,String> map ){
        String msg="修改失败";
        boolean flag = userInfoService.updatePhoto(map);
        if(flag){
            msg="修改成功";
        }
        return msg;
    }

    @RequestMapping("/addMoney")
    public String addMoney(@RequestParam Map<String, String> map) {
        boolean isSucceed = userInfoService.addMoney(map);
        if (isSucceed) {

            Map<String, Object> budget = new HashMap<>();
            int id = Integer.parseInt(map.get("userId"));
            budget.put("userId", id);
            budget.put("itemType", "充值");
            budget.put("itemMoney", map.get("money"));
            budget.put("currentUseBalance", userInfoService.getUserAccount(id));

            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateTime = formatter.format(calendar.getTime());
            budget.put("itemDateTime", dateTime);
            budgetInfoController.createBudget(budget);

            return "充值成功";
        }
        return "充值失败！";
    }

    @RequestMapping("/updateUserPwd")
    public boolean updatePwd(@RequestParam Map<String,String> map){
        return userInfoService.updatePwdByName(map);
    }
}
